package com.sharemarking.application.common.util;

import java.io.FileOutputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;

@SuppressWarnings("restriction")
public class Util {

	// base64字符串转化成图片

    public static boolean GenerateImage(String imgStr,String fileName) {
        // 对字节数组字符串进行Base64解码并生成图片

        if (imgStr == null||imgStr.equals(""))
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码

            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据

                    b[i] += 256;
                }
            }
            // 生成新的图片

            //String imgFilePath = "upload/xxx.png";

            OutputStream out = new FileOutputStream(fileName);
            out.write(b);
            out.flush();  
            out.close();  
            return true;
        } catch (Exception e) {
            return false;
        }
    }
	
}